﻿
using MD5Hash;
using Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace APIDAL
{
    public class UserDAL
    {
        public UserDAL(MyContext context)
        {
            Context = context;
        }

        public MyContext Context { get; }

        public int AddUser(User user )
        {
            user.Password = user.Password.GetMD5();
            Context.Users.Add( user );
            return Context.SaveChanges();
        }

        public User GetUser(string username,string pwd) {
            var u = Context.Users.FirstOrDefault(x => x.Name == username);
            if( u == null)
            {
                u = new User { Id = -1 };
            }
            else
            {
                if( u.Password != pwd.GetMD5())
                {
                    u = new User { Id = -2 };
                }
            }
            return u;
        }
    }
}
